// pages/getYouhui/getYouhui.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		youhuiquan: [
			//  { mianzhi: 20, lastDate: '2018-06-20', minMoney: 100, isGet: false},
			//  { mianzhi: 50, lastDate: '2018-06-30', minMoney: 200, isGet: false},
			//  { mianzhi: 100, lastDate: '2018-06-30', minMoney: 500, isGet: false},
			//  { mianzhi: 120, lastDate: '2018-06-30', minMoney: 600, isGet: false }
		],
		time: {},
		id: ""
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var that = this;
		var id = options.id;
		console.log(app.globalData.coupon);
		// 优惠券列表
		wx.request({
			url: app.globalData.directory + 'api/v2/getCoupons/' + id, //仅为示例，并非真实的接口地址
			data: {

			},
			header: {
				'content-type': 'application/json', // 默认值
				'token': wx.getStorageSync('token')
			},
			success: function (res) {
				if (res.data.status == 'ok') {
					// console.log('22', res.data);
					that.setData({
						youhuiquan: res.data.data.coupons,
						time: res.data.data.time,
						id: id
					})
				} else {
					wx.showToast({
						title: '请求失败,请稍候',
						icon: 'none',
						duration: 2000,
					})
				}
			}
		});

	},

	getYouhui: function (e) {
		//  let index = e.currentTarget.dataset.index
		//  let youhuiquan = this.data.youhuiquan
		//  let isGet = youhuiquan[index].isGet


		// if(youhuiquan[index].isGet == false){
		//   wx.showToast({
		//     title: '领取成功!',
		//   })
		//   youhuiquan[index].isGet = !isGet
		//   console.log(isGet)
		// }else{
		//   wx.showToast({
		//     title: '您已领过!',
		//   })
		//   return 
		// }
		//  this.setData({
		//    youhuiquan:youhuiquan
		//  })
		// console.log(e.currentTarget.dataset.id)
		var id = e.currentTarget.dataset.id;
		// console.log(this.data.id)
		var that = this
		// console.log(id)
		// 优惠券领取
		console.log(app.globalData.coupon);
		wx.request({
			url: app.globalData.directory + 'api/v2/getCoupon',
			method: 'POST',
			data: {
				activity_id: that.data.id,
				coupon_id: id
			},
			header: {
				'content-type': 'application/json', // 默认值
				'token': wx.getStorageSync('token'),
			},
			success: function (res) {
				if (res.data.status == 'ok') {
					// console.log('22', res.data);
					// app.globalData.coupon.data.push(res.data);
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
					});


					wx.request({
						url: app.globalData.directory + 'api/v2/pay/facepay',
						header: {
							'content-type': 'application/json',
							'token': wx.getStorageSync('token')
						},
						success: function (res) {
							// console.log(res)
							if (res.errMsg == 'request:ok') {
								app.globalData.coupon = res.data.coupon;
								app.globalData.points = res.data.points;

							} else {
								wx.showToast({
									title: '请求失败,请重试',
									icon: 'none',
									duration: 2000,
								})
							}
						}
					})
					console.log(app.globalData.coupon,12213123123123123);

				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
					})
				}
			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})